package com.mllweb.blog.web.controller;

import com.mllweb.blog.base.BaseController;
import com.mllweb.blog.core.digest.Base64Utils;
import com.mllweb.blog.core.digest.RSAUtils;
import com.mllweb.blog.data.orm.BlogUser;
import com.mllweb.blog.data.orm.BlogUserLogin;
import com.mllweb.blog.data.orm.BlogUserValidate;
import com.mllweb.blog.service.RedisService;
import com.mllweb.blog.service.UserService;
import com.mllweb.blog.service.ValidateService;
import com.mllweb.blog.service.state.LoginState;
import com.mllweb.blog.service.state.ValidateState;
import com.mllweb.blog.web.session.SessionKey;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.jws.WebParam;
import java.util.Date;

/**
 * 用户Controller
 */
@Controller
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private ValidateService validateService;
    @Autowired
    private RedisService redisService;

    /**
     * 跳转注册界面
     */
    @RequestMapping(value = "register", method = RequestMethod.GET)
    private String register() {
        return "user/register";
    }

    /**
     * 处理注册请求
     */
    @RequestMapping(value = "register", method = RequestMethod.POST)
    private String register(BlogUser user, BlogUserLogin userLogin, Model model) {
        userService.register(user, userLogin);
        model.addAttribute("status", ValidateState.SENDED);
        model.addAttribute("mailWeb", redisService.getEmailWeb(userLogin.getLoginName()));
        return "user/activate";
    }

    /**
     * 处理邮箱激活请求
     */
    @RequestMapping(value = "activate/{email}/{sign}", method = RequestMethod.GET)
    private String activate(@PathVariable String email, @PathVariable String sign, Model model) {
        ValidateState state = validateService.validate(email, sign);
        model.addAttribute("status", state.toString());
        return "user/activate";
    }

    /**
     * 跳转登录界面
     */
    @RequestMapping(value = "login", method = RequestMethod.GET)
    private String login() {
        return "user/login";
    }

    /**
     * 处理登录请求
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    private String login(BlogUserLogin userLogin, boolean remember) {
        LoginState state = userService.login(userLogin);
        if (state == LoginState.SUCCESS) {
            return redirect("/");
        }
        return "user/login";
    }

    /**
     * 跳转注册界面
     */
    @RequestMapping(value = "user/{base64Id}", method = RequestMethod.GET)
    private String home(@PathVariable String base64Id) {
        try {
            int id = Integer.parseInt(Base64Utils.decodeAsHex(base64Id));
        } catch (Exception e) {
            errorStatus(HttpStatus.NOT_FOUND);
        }
        return "user/home";
    }

    @RequestMapping(value = "logout", method = RequestMethod.GET)
    private String logout() {
        this.session.setAttribute(SessionKey.USER.toString(), null);
        return redirect("/");
    }
}
